package service;

import java.util.List;

import dao.GenericDAO;
import exception.InvalidBusinessStateException;

public abstract class GenericService<E> {
	
	protected GenericDAO<E> dao;
	
	public E store(E entity) throws InvalidBusinessStateException {
		businessRules(entity);
		handleDependencies(entity);
		return dao.store(entity);
	}
	
	public void delete(Integer id) throws InvalidBusinessStateException {
		cascadeDeletion(id);
		dao.delete(id);
	}
	
	public List<E> getAll() {
		return dao.getAll();
	}
	
	public E update(E entity) throws InvalidBusinessStateException {
		businessRules(entity);
		handleDependencies(entity);
		return dao.update(entity);
	}
	
	public E find(Integer id) {
		if (id == null) throw new RuntimeException("ID CANNOT BE NULL OMFG");
		return dao.find(id);
	}
	
	protected abstract void businessRules(E entity) throws InvalidBusinessStateException;
	protected abstract void handleDependencies(E entity) throws InvalidBusinessStateException;
	protected abstract void cascadeDeletion(Integer id) throws InvalidBusinessStateException;
}
